import logging
import unittest
import requests
# 导入mysql
from unit__001.unit_06.Mysql import Mysql
from unit__001.unit_06.Tools import Tools


class WeiDu(unittest.TestCase):
    def setUp(self) -> None:
        print('开始')
        # 初始化数据库
        self.db = Mysql(host='10.59.9.19', user='yidongceshi', password='Yd123456!', database='movie')
        Tools().Log4()
        logging.debug('debug level')
        logging.info('info level')
        logging.error('error level')
        logging.critical('critical level')
        logging.warning('warning level')

    def tearDown(self) -> None:
        print('结束')
        self.db.close()

    # 登录
    sessionId = None
    userId = None
    nickName = None
    email = None

    def test_001(self):
        logging.info('start test_001')
        r1 = requests.post(url='http://10.59.9.18/movieApi/user/v2/login',
                           params={'email': '3601552946@qq.com', 'pwd': 'eWLPHopE945d2ivttHaQTQ=='})
        print(r1.json())
        # 提取响应中的sessionId、userId
        WeiDu.sessionId = r1.json()['result']['sessionId']
        WeiDu.userId = r1.json()['result']['userId']
        # print(WeiDu.userId)
        # 数据库断言1 对比响应中的nickName和数据库中的nickName是否一致
        WeiDu.nickName = r1.json()['result']['userInfo']['nickName']
        print('响应中的nickName：', WeiDu.nickName)
        sq1 = f'select nick_name from user where id={str(WeiDu.userId)}'
        result1 = self.db.assertEqual(sq1, WeiDu.nickName)
        print('数据库中返回的值：', result1)
        # self.assertEqual(WeiDu.nickName, result1[0][0])
        # 数据库断言2 对比响应中的email和数据库中的email是否一致
        WeiDu.email = r1.json()['result']['userInfo']['email']
        print('响应中的email：', WeiDu.email)
        sq2 = f'select email from user where id={str(WeiDu.userId)}'
        result2 = self.db.assertIn(sq2, WeiDu.email)
        # self.assertEqual(WeiDu.email, result2[0][0])
        print('数据库中返回的email值：', result2)
        # 数据库断言3 对比响应中的sex和数据库中的sex是否一致
        WeiDu.sex = r1.json()['result']['userInfo']['sex']
        print('响应中的sex：', WeiDu.sex)
        sq3 = f'select sex from user where id={str(WeiDu.userId)}'
        result3 = self.db.asserteTrue(sq3, WeiDu.sex)
        # self.assertEqual(WeiDu.sex, result3[0][0])
        print('数据库中返回的sex值：', result3)
        logging.info('end test_001')

    # 查询热门电影
    movieId = None
    director = None
    dyname = None

    def test_002(self):
        logging.info('start test_002')
        r2 = requests.get(url='http://10.59.9.18/movieApi/movie/v2/findHotMovieList',
                          params={'page': 1, 'count': 5})
        print(r2.json())
        # 找打热门电影id
        WeiDu.movieId = r2.json()['result'][1]['movieId']
        # print(WeiDu.movieId)
        # 数据库断言1-江湖儿女 对比响应中的电影名称是否在数据库中存在
        WeiDu.dyname = r2.json()['result'][1]['name']
        print('响应中的电影名称：',WeiDu.dyname)
        sql1 = f'select name from movies where id={str(WeiDu.movieId)}'
        result1 = self.db.assertNotEqual(sql1, WeiDu.movieId)
        # self.assertTrue(WeiDu.dyname == result1[0][0])
        print('数据库中返回的值：', result1)
        # 数据库断言2-贾樟柯 对比响应中的director的值是否和数据库中的一致
        WeiDu.director = r2.json()['result'][1]['director']
        print('响应中的director：', WeiDu.director)
        sql2 = f'select director from movies where id={str(WeiDu.movieId)}'
        result2 = self.db.assertNotIn(sql2, str(WeiDu.dyname))
        # self.assertEqual(WeiDu.director, result2[0][0])
        print('数据库中返回的值：', result2)
        # 数据库断言3-8.6 对比响应中对应的电影名称的价格，是否在数据库中对应的电影名的价格一致
        WeiDu.score = r2.json()['result'][1]['score']
        print('响应中的score：', WeiDu.score)
        sql3 = f'select score from movies where id={str(WeiDu.movieId)}'
        result3 = self.db.asserteFalse(sql3,WeiDu.director)
        # self.assertEqual(WeiDu.score, result3[0][0])
        print('数据库中返回的值：', result3)
        logging.info('end test_002')

    # 查询区域列表
    regionId = None
    regionName = None
    regionId1 = None
    regionName1 = None

    def test_003(self):
        logging.info('start test_003')
        r3 = requests.get(url='http://10.59.9.18/movieApi/tool/v2/findRegionList')
        print(r3.json())
        WeiDu.regionId = r3.json()['result'][6]['regionId']
        # 数据库断言1-海淀区 根据regionId去数据库中查询regionName 对比响应中的和数据库中的是否一致
        WeiDu.regionName = r3.json()['result'][6]['regionName']
        result1 = self.db.chaxun(f'select region_name from region where id={str(WeiDu.regionId)}')
        self.assertEqual(WeiDu.regionName, result1[0][0])
        print(result1)
        logging.info('end test_003')

    # 根据电影id,区域id 查询播放影院信息
    cinemaId = None
    address = None
    name = None
    logo = None

    def test_004(self):
        logging.info('start test_004')
        r4 = requests.get(url='http://10.59.9.18/movieApi/movie/v2/findCinemasInfoByRegion',
                          params={'movieId': WeiDu.movieId, 'regionId': WeiDu.regionId,
                                  'page': 1, 'count': 5})
        print(r4.json())
        # 提取影院Id
        WeiDu.cinemaId = r4.json()['result'][4]['cinemaId']
        print(WeiDu.cinemaId)
        self.assertTrue('复兴路69号五棵松卓展时代百货五层东侧', WeiDu.address)
        # 数据库断言1-我的最新签名子 对比响应中提取的name和数据库中的name的是否一致
        WeiDu.name = r4.json()['result'][4]['name']
        print(WeiDu.name)
        result1 = self.db.chaxun(f'select name from cinemas where id={str(WeiDu.cinemaId)}')
        self.assertEqual(WeiDu.name, result1[0][0])
        print(result1)
        # 数据库断言2-中关村广场购物中心津乐汇三层（鼎好一期西侧）
        # 对比响应中提取的name和数据库中的name的是否一致
        WeiDu.address = r4.json()['result'][4]['address']
        result2 = self.db.chaxun(f'select address from cinemas where id={str(WeiDu.cinemaId)}')
        self.assertEqual(WeiDu.address, result2[0][0])
        # 数据库断言3-'logo' 对比响应中提取的logo和数据库中的logo的是否一致
        WeiDu.logo = r4.json()['result'][4]['logo']
        result3 = self.db.chaxun(f'select logo from cinemas where id={str(WeiDu.cinemaId)}')
        self.assertEqual(WeiDu.logo, result3[0][0])
        logging.info('end test_004')

    # 根据电影ID和影院ID查询电影排期列表
    hallId = None
    pqid = None
    screeningHall = None
    endTime = None

    def test_005(self):
        r5 = requests.get(url='http://10.59.9.18/movieApi/movie/v2/findMovieSchedule',
                          params={'movieId': WeiDu.movieId, 'cinemaId': WeiDu.cinemaId})
        print(r5.json())
        WeiDu.hallId = r5.json()['result'][0]['hallId']
        # 提取排期id
        WeiDu.pqid = r5.json()['result'][0]['id']
        # 数据库断言1-2号激光厅 对比响应中提取的screeningHall和数据库中的screening_hall的是否一致
        WeiDu.screeningHall = r5.json()['result'][0]['screeningHall']
        result1 = self.db.chaxun(f'select screening_hall from movie_schedule where hall_id={str(WeiDu.hallId)}')
        self.assertEqual(WeiDu.screeningHall, result1[0][0])

    # 购票下单
    message = None
    orderId = None

    def test_006(self):
        r6 = requests.post(url='http://10.59.9.18/movieApi/movie/v2/verify/buyMovieTickets',
                           headers={'userId': str(WeiDu.userId), 'sessionId': str(WeiDu.sessionId)},
                           json={'scheduleId': WeiDu.pqid, 'seat': ["2-4", "2-9"],
                                 'sign': 'c81846af6e761ddb5dc127569feeab24'})
        print(r6.json())
        # 断言 message 网络异常
        WeiDu.message = r6.json()['message']
        self.assertNotEqual('下单成功！', WeiDu.message)
        # 提取 orderId
        WeiDu.orderId = r6.json()['orderId']
        print(WeiDu.orderId)
        # 数据库断言1-orderId 响应中提取的orderId对比和数据库中的orderId是否生成订单
        result1 = self.db.chaxun(f'select user_id from user_buy_ticket_record where order_id={str(WeiDu.orderId)}')
        self.assertEqual(WeiDu.userId, result1[0][0])
        # 数据库断言1-orderId 响应中提取的orderId对比和数据库中的orderId是否生成订单
        result2 = self.db.chaxun(f'select movie_name from user_buy_ticket_record where order_id={str(WeiDu.orderId)}')
        print(result2[0][0])
        self.assertEqual(WeiDu.dyname, result2[0][0])

    # 支付
    result_zf = None

    def test_007(self):
        r7 = requests.post(url='http://10.59.9.18/movieApi/movie/v2/verify/pay',
                           headers={'userId': str(WeiDu.userId), 'sessionId': str(WeiDu.sessionId)},
                           json={'payType': 1, 'orderId': str(WeiDu.orderId)})
        print(r7.json())


if __name__ == '__main__':
    unittest.main()
